package com.j1903.controller;

import com.j1903.util.JsonResult;
import com.j1903.util.SendEmail;
import com.j1903.vo.EmailParamVo;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Random;

@RestController
@RequestMapping("/email")
public class EmailController {
    @RequestMapping("/send")
    public JsonResult send(
            @RequestBody EmailParamVo email){
        String contentType=email.getContentType();
        boolean rs= SendEmail.send(email.getRecEmail(),
                email.getSubject(),email.getContent(),
                contentType==null?"text/html;charset=UTF-8":contentType);
        if(rs){
            return new JsonResult(200, "发送成功", null);
        }else{
            return new JsonResult(500, "发送失败", null);
        }
    }
    @RequestMapping("/front/sendCode")
    public JsonResult sendCode(String email){
        String token=SendEmail.sendAuthCode(email);
        if(token!=null){
            return new JsonResult(200, "发送邮件验证码成功", token);
        }else{
            return new JsonResult(500, "发送邮件验证码失败", null);
        }
    }
}
